/*
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  Author: hakanai
 *
 */

#include <stdio.h>
#include <stdint.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <string.h>
#include "esp_err.h"
#include "esp_log.h"

#include <pn532hsu.h>

static const char *TAG = "app";

static void printbuf(uint8_t *buffer, uint16_t len) {
  for(uint16_t i = 0; i < len; i++){
    printf("%.2x ", buffer[i]);
  }
}

void app_main(void) {
  int len;
  uint8_t buf[64] = { 0 };
  uint8_t card[64] = { 0 };
  ESP_ERROR_CHECK(pn532_init());

  while (1) {
    if ((len = pn532_read(buf, 64)) != ESP_FAIL) {
/*      printbuf(buf, len);*/
/*      puts("");*/
      len = pn532_decode_card(buf, len, card);
      printf("Card Detected: ");
      printbuf(card, len);
      puts("");
      vTaskDelay(500 / portTICK_PERIOD_MS);
    }

    vTaskDelay(100 / portTICK_PERIOD_MS);
  }
}

